import 'package:chat_app_project/screens/circle/circle.dart';
import 'package:chat_app_project/screens/index/Index.dart';
import 'package:chat_app_project/screens/index/publish.dart';
import 'package:chat_app_project/screens/login/login.dart';
import 'package:chat_app_project/screens/tabIcon_data.dart';
import 'package:chat_app_project/screens/wk/conversation.dart';
import 'package:chat_app_project/util/toast_util.dart';
import 'package:flutter/material.dart';

import '../tool/event_bus.dart';
import '../tool/im.dart';
import '../util/sp_util.dart';
import 'bottom_app_bar_4.dart';
import 'person/profile.dart';

class HomeScreen extends StatefulWidget {
  int selectedIndex = 0;

  HomeScreen({super.key, required this.selectedIndex});

  @override
  State<HomeScreen> createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
  int _selectedIndex = 0;

  @override
  void initState() {
    super.initState();
    _selectedIndex = widget.selectedIndex;

    f(param) {
      // 移除事件监听
      bus.off("logout");
      SpUtil.clear();
      // 跳转登录页面
      ToastUtil.toast("未登录或登录失效,请重新登录");
      // Navigator.of(context).pushNamedAndRemoveUntil('/login', (route) => false);
      //延时一秒过渡
      Future.delayed(const Duration(seconds: 1), () {
        //断开IM连接
        IMUtils.disconnect();
        Navigator.of(context).pushReplacement(MaterialPageRoute(builder: (context)=>const LoginPage()));
        // 重新添加事件监听
        bus.on("logout", f);
      });
    }
    bus.on("logout", f);
  }

  var list = [
    const Index(),
    const Circle(),
    const ConversationPage(),
    const ProfilePage(),
  ];

  List<TabIconData> tabIconsList = TabIconData.tabIconsList;

  Widget bottomBar() {
    return Positioned(
        left: 0,
        right: 0,
        bottom: 0,
        child: BottomAppBar4(
          tabIconsList: tabIconsList,
          centerClick: () {
            // 按钮动画结束之后
            debugPrint('点击了中间按钮');
            Navigator.of(context).push(MaterialPageRoute(
                builder: (context) => const Publish()));
          },
          changeIndex: (index) => onClickBottomBar(index),
        ));
  }

  void onClickBottomBar(int index) {
    if (!mounted) return;
    setState(() => _selectedIndex = index);
  }

  BottomNavigationBar buildBottomNavigationBar() {
    return BottomNavigationBar(
      currentIndex: _selectedIndex,
      onTap: (value) {
        setState(() {
          _selectedIndex = value;
        });
      },
      // type: BottomNavigationBarType.fixed,
      items: const [
        BottomNavigationBarItem(icon: Icon(Icons.home), label: '首页'),
        BottomNavigationBarItem(icon: Icon(Icons.circle_outlined), label: '小圈'),
        BottomNavigationBarItem(icon: Icon(Icons.message), label: '消息'),
        BottomNavigationBarItem(
            icon: Icon(Icons.perm_identity_outlined), label: '我的'),
        // BottomNavigationBarItem(
        //     icon: CircleAvatar(
        //       radius: 16,
        //       backgroundImage: AssetImage('assets/images/icon_im.png'),
        //     ),
        //     label: '我的'),
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      // appBar: buildAppBar(),

      body: Stack(
        children: [
          IndexedStack(
            index: _selectedIndex,
            children: list,
          ),
          bottomBar(),
        ],
      ),

      // body: IndexedStack(
      //   index: _selectedIndex,
      //   children: list,
      // ),
      // floatingActionButton: FloatingActionButton(
      //   onPressed: (() {}),
      //   backgroundColor: kPrimaryColor,
      //   child: const Icon(Icons.person_add_alt_1, color: Colors.white),
      // ),
      // bottomNavigationBar: buildBottomNavigationBar()
    );
  }
}
